﻿using System;
using System.Windows.Forms;
using Sedge.Core.Globalization;
using Sedge.UI.Controller;
using Sedge.UI.ViewModel;

namespace Sedge.UI.View
{
	public partial class ContactsView : UserControl, ITransferable
	{
		private readonly ContactsViewModel _contactsViewModel;

		public ContactsView()
		{
			InitializeComponent();
		}

		public ContactsView(ContactsViewModel contactsViewModel)
			: this()
		{
			_contactsViewModel = contactsViewModel;
			SetupWindow();
		}

		private void SetupWindow()
		{
			labelFirstName.Text = Locale.Strings.ContactsFirstName;
			int colWidth = labelFirstName.Right;

			labelLastName.Text = Locale.Strings.ContactsLastName;
			colWidth = Math.Max(colWidth, labelLastName.Right);

			labelCompany.Text = Locale.Strings.ContactsCompany;
			colWidth = Math.Max(colWidth, labelCompany.Right);

			labelEmail.Text = Locale.Strings.ContactsEmail;
			colWidth = Math.Max(colWidth, labelEmail.Right);

			labelPhone.Text = Locale.Strings.ContactsPhone;
			colWidth = Math.Max(colWidth, labelPhone.Right);

			labelFile.Text = Locale.Strings.ContactsFile;
			colWidth = Math.Max(colWidth, labelFile.Right);

			textFirstName.Left = colWidth;
			textLastName.Left = colWidth;
			textCompany.Left = colWidth;
			textEmail.Left = colWidth;
			textPhone.Left = colWidth;
			textFile.Left = colWidth;
			
			labelNotes.Left = colWidth;
			labelNotes.Text = _contactsViewModel.Notes;

			buttonBrowse.Text = Locale.Strings.ContactsBrowse;
			buttonBrowse.Left = textFile.Right + 5;

		}

		public void TransferData()
		{
			_contactsViewModel.StoreData(textFirstName.Text, textLastName.Text,
				textCompany.Text, textEmail.Text, textPhone.Text, textFile.Text);
		}

		private void buttonBrowse_Click(object sender, EventArgs e)
		{
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				textFile.Text = openFileDialog.FileName;
			}
		}
	}
}
